<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
  <style type="text/css">
    html {}

    body {
      margin: 0;
      background: #EEE;
    }

    h2 {
      text-align: center;
      font-family: 'Microsoft Yahei';
    }

    button {
      float: right;
    }

    .demo-list {
      width: 300px;
      margin: 10px auto;
    }

    .demo-list-item {
      background: #FAFAFA;
      border: solid 5px #DDD;
      border-radius: 15px;
      padding: 15px;
      margin-top: 15px;
    }
  </style>
  <script type="text/javascript">
    function copy(domId) {
      if (copyDom(document.getElementById(domId))) {
        alert('复制成功');
      } else {
        alert('当前浏览器不支持');
      }
    }

    function copyDom(dom) {
      if ('clipboardData' in window) {
        window.clipboardData.setData('Text', dom.value || dom.innerText || dom.innerHTML);
      } else if ('execCommand' in document && 'getSelection' in window) {
        var domType = 'input';
        try {
          dom.select();
        } catch (e) {
          domType = 'text';
          window.getSelection().selectAllChildren(dom);
        }
        document.execCommand("Copy");
        if (domType == 'input') {
          dom.blur();
        } else {
          window.getSelection().empty();
        }
      } else {
        return false;
      }
      return true;
    }
  </script>
</head>

<body>
  <div class="demo-list">
    <div class="demo-list-item">
      <button onclick="copy('demo_div');">复制</button>
      <div id="demo_div">DIV</div>
    </div>
    <div class="demo-list-item">
      <button onclick="copy('demo_input');">复制</button>
      <input id="demo_input" value="INPUT"><br>(不支持iPhone)
    </div>
    <div class="demo-list-item">
      <button onclick="copy('demo_textarea');">复制</button>
      <textarea id="demo_textarea">TEXTAREA</textarea><br>(不支持iPhone)
    </div>
    <div class="demo-list-item">
      <button onclick="copy('demo_divs');">复制</button>
      <div id="demo_divs">
        <p>DIVS</p>
        <p>DIVS</p>
      </div>
    </div>

  </div>
</body>

</html>